home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / comstr.exe / COMSTREA.H < prev    next >
C/C++ Source or Header  |  1992-12-01  |  4KB  |  220 lines

  1. #if !defined( __COMSTREAM_H )
  2. #define __COMSTREAM_H
  3.  
  4. #if !defined( __IOSTREAM_H )
  5. #include <Iostream.h>
  6. #endif    // __IOSTREAM_H
  7.  
  8. #define NUMBEROFPORTS    4
  9.  
  10. #define TIMEOUT        1
  11. #define OR            2
  12. #define PE            4
  13. #define FE            8
  14. #define BI            16
  15.  
  16. #define    DTR    1
  17. #define MCR    4
  18.  
  19. #define HW_HANDSHAKE    1
  20. #define SW_HANDSHAKE    2
  21. #define WAITFORECHO        4
  22.  
  23. #define NO_PARITY    0
  24. #define ODD_PARITY    1
  25. #define EVEN_PARITY    2
  26.  
  27. class combuf;
  28.  
  29. struct ComDef
  30.     {
  31.     unsigned portAddr;
  32.     unsigned char irqAddr;
  33.     unsigned char picMask;
  34.  
  35.     ComDef( int );
  36.     ~ComDef();
  37.  
  38.     void setParams( int, int, int );
  39.  
  40.     protected:
  41.         friend class combuf;
  42.  
  43.         int  setCombuf( combuf * );
  44.         void clrCombuf();
  45.  
  46.         void interrupt far (*oldhandler)(...);
  47.         combuf *owner;
  48.     };
  49.  
  50. struct CommProtocol
  51.     {
  52.     long baud;
  53.     int bits, parity, stopBits;
  54.  
  55.     CommProtocol( long, int, int, int );
  56.     };
  57.  
  58. inline CommProtocol::CommProtocol( long b, int bt, int p, int sb ) : baud(b), bits(bt),
  59.         parity(p), stopBits(sb)
  60.     {}
  61.  
  62. class combuf : public streambuf
  63.     {
  64.     public:
  65.         static ComDef def[NUMBEROFPORTS];
  66.  
  67.         enum ParityType {    none, odd, even };
  68.  
  69.         combuf(char *, int, int );
  70.         combuf();
  71.         ~combuf();
  72.  
  73.         virtual    streambuf *setbuf( signed char *, int );
  74.         combuf *setbuf( char *, int, int );
  75.  
  76.         virtual    int underflow();
  77.  
  78.         virtual int do_sputn( const char *, int );
  79.         virtual int overflow( int = EOF );
  80.  
  81.         void setPort( int = EOF );
  82.         void setTimeout( unsigned long );
  83.         void setInterCharDelay( unsigned long );
  84.         void setInterLineDelay( unsigned long );
  85.  
  86.         void setOption( int );
  87.         void clearOption( int );
  88.  
  89.         void setBaud( long );
  90.         void setParity( int );
  91.         void setBits( int );
  92.         void setStopBits( int );
  93.         void setParams( CommProtocol& );
  94.  
  95.         void assertDTR();
  96.         void unassertDTR();
  97.  
  98.         int  status();
  99.  
  100.         friend class comstream;
  101.  
  102.     protected:
  103.         friend struct ComDef;
  104.  
  105.         int offs_;
  106.         int errFlags,options;
  107.         int inSize;
  108.  
  109.         unsigned long timeout_;
  110.         unsigned long ibdelay_;
  111.         unsigned long ildelay_;
  112.  
  113.         int extractc();
  114.         int insertc( int );
  115.  
  116.         unsigned long timeToTicks( unsigned long );
  117.  
  118.         void assertToPort( int, int );
  119.         void unassertToPort( int, int );
  120.  
  121.     static void interrupt far handler0x0B(...);
  122.     static void interrupt far handler0x0C(...);
  123.         void handleIRQ( int, int, int );
  124.     };
  125.  
  126. inline void combuf::assertDTR()
  127.     {
  128.     if( offs_ != EOF )
  129.         assertToPort( MCR, DTR );
  130.     }
  131.  
  132. inline void combuf::unassertDTR()
  133.     {
  134.     if( offs_ != EOF )
  135.         unassertToPort( MCR, DTR );
  136.     }
  137.  
  138. inline int combuf::status()
  139.     {
  140.     int n = errFlags;
  141.     errFlags = 0;
  142.     return n;
  143.     }
  144.  
  145. inline void combuf::setOption( int i )
  146.     {
  147.     options |= i;
  148.     }
  149.  
  150. inline void combuf::clearOption( int i )
  151.     {
  152.     options &= ~i;
  153.     }
  154.  
  155. class _CLASSTYPE comstream : public iostream
  156.     {
  157.     public:
  158.  
  159.         _Cdecl comstream();
  160.         _Cdecl comstream( char _FAR *, int, int );
  161.         _Cdecl ~comstream();
  162.  
  163.         combuf _FAR * _Cdecl rdbuf();
  164.         void _Cdecl open( int );
  165.         void _Cdecl close();
  166.  
  167.     private:
  168.  
  169.         combuf buf;
  170.     };
  171.  
  172. inline combuf *comstream::rdbuf()
  173.     {
  174.     return &buf;
  175.     }
  176.  
  177. typedef comstream& (*CSLongManip)(comstream&, long);
  178. typedef comstream& (*CSIntManip)(comstream&, int);
  179.  
  180. class ComManipLong
  181.     {
  182.     public:
  183.         ComManipLong( CSLongManip f, long l) : fn(f), arg(l)    {}
  184.  
  185.         friend comstream& _Cdecl operator<<(comstream& cs, ComManipLong& f )
  186.             {
  187.             return (*f.fn)(cs, f.arg);
  188.             }
  189.  
  190.     private:
  191.         CSLongManip fn;
  192.         long arg;
  193.     };
  194.  
  195. class ComManipInt
  196.     {
  197.     public:
  198.         ComManipInt( CSIntManip f, int i) : fn(f), arg(i)    {}
  199.  
  200.         friend comstream& _Cdecl operator<<(comstream& cs, ComManipInt& f )
  201.             {
  202.             return (*f.fn)(cs, f.arg);
  203.             }
  204.  
  205.     private:
  206.         CSIntManip fn;
  207.         int arg;
  208.     };
  209.  
  210. ComManipLong cdecl baud( long );
  211. ComManipInt  cdecl bits( int );
  212. ComManipInt  cdecl parity( int );
  213. ComManipInt  cdecl stopbits( int );
  214. ComManipLong cdecl timeout( long );
  215. ComManipInt  cdecl intercharDelay( int );
  216. ComManipInt  cdecl interlineDelay( int );
  217.  
  218.  
  219. #endif    // __COMSTREAM_H
  220.